package zxing.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.githang.statusbar.StatusBarCompat;
import com.google.gson.Gson;

import java.util.Collections;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.virtualapp.R;
import io.virtualapp.abs.ui.VActivity;
import io.virtualapp.bean.DataResultBean;
import io.virtualapp.bean.ResultBean;
import io.virtualapp.bean.ServiceDetailBean;
import io.virtualapp.home.adapters.ImageSeeAdapter;
import io.virtualapp.mylibrary.callback.BaseCallback;
import io.virtualapp.mylibrary.util.HttpUtils;
import io.virtualapp.mylibrary.util.ToastUtils;
import okhttp3.Request;
import okhttp3.Response;
import zxing.activity.adapter.ReplyBeanAdapter;

public class SuDetailsActivity extends VActivity {

    @BindView(R.id.iv_dis)
    ImageView ivDis;
    @BindView(R.id.tv_title2)
    TextView tvTitle;
    @BindView(R.id.cl_)
    ConstraintLayout cl;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_state)
    TextView tvState;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_title)
    TextView tvContenTitle;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.rc_imag)
    RecyclerView rcImag;
    @BindView(R.id.rc_chat)
    RecyclerView rcChat;
    @BindView(R.id.bt_bottom)
    Button btBottom;
    @BindView(R.id.ns)
    NestedScrollView ns;

    @BindView(R.id.progressBar2)
    ProgressBar progressBar2;
    @BindView(R.id.tv_end)
    TextView tvEnd;
    @BindView(R.id.relativeLayout)
    RelativeLayout relativeLayout;

    private int service_id = -1;

    private ImageSeeAdapter seeAdapter;
    private ReplyBeanAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.su_activity_su_details);

        ButterKnife.bind(this);

        initView();
        initData();
    }

    private void initData() {
        service_id = getIntent().getIntExtra("data", -1);
        if (service_id != -1) {
            getDetailsFormNet(service_id);
        }
    }

    private void initView() {
        btBottom.setText("回复");
        btBottom.setOnClickListener(view -> {
            Intent intent = new Intent(SuDetailsActivity.this, SuRebackActivity.class);
            intent.putExtra("data", service_id);
            startActivityForResult(intent, 121);
        });

        ivDis.setOnClickListener(view -> finish());
        seeAdapter = new ImageSeeAdapter(null);
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        rcImag.setLayoutManager(manager);
        rcImag.setAdapter(seeAdapter);
        rcChat.setNestedScrollingEnabled(false);
        rcImag.setNestedScrollingEnabled(false);

        LinearLayoutManager manager2 = new LinearLayoutManager(this);
        rcChat.setLayoutManager(manager2);
        adapter = new ReplyBeanAdapter(null);
        rcChat.setAdapter(adapter);

        seeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(SuDetailsActivity.this, ImagesVIewActivity.class);
                intent.putExtra(ImagesVIewActivity.IMG_POS, position);
                Gson gson = new Gson();
                intent.putExtra(ImagesVIewActivity.IMG_DATAS, gson.toJson(seeAdapter.getData()));
                startActivity(intent);
            }
        });

        tvEnd.setOnClickListener(view -> {
            endService();
        });
    }

    private void endService() {
        HttpUtils.getInstance().postEndService(service_id, new BaseCallback<ResultBean>() {
            @Override
            public void onRequestBefore() {
                progressBar2.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFailure(Request request, Exception e) {
                progressBar2.setVisibility(View.GONE);
            }

            @Override
            public void onSuccess(Response response, ResultBean o) {
                progressBar2.setVisibility(View.GONE);
                if (o!=null&&o.isIssucc()){
                    ToastUtils.showShortToast("服务已结束");
                    SuDetailsActivity.this.setResult(RESULT_OK);
                    SuDetailsActivity.this.finish();
                }
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                progressBar2.setVisibility(View.GONE);

            }
        });
    }

    private void getDetailsFormNet(int service_id) {
        HttpUtils.getInstance().postGetServicesDetails(service_id, new BaseCallback<DataResultBean<ServiceDetailBean>>() {
            @Override
            public void onRequestBefore() {
                progressBar2.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFailure(Request request, Exception e) {
                progressBar2.setVisibility(View.GONE);
            }

            @Override
            public void onSuccess(Response response, DataResultBean<ServiceDetailBean> o) {
                progressBar2.setVisibility(View.GONE);
                if (o != null && o.getIssucc()) {
                    setBena2View(o.getData());
                }
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                progressBar2.setVisibility(View.GONE);
            }
        });
    }

    private void setBena2View(ServiceDetailBean data) {
        if (data == null) {
            ToastUtils.showShortToast("数据无效");
            return;
        }
        tvTime.setText(data.getAddtime());
        String temp = "待回复";
        switch (data.getStatus()) {
            case 0:
            case 1:
                temp = "待回复";
                break;
            case 2:
                temp = "已回复";
                break;
            case 99:
                temp = "已解决";
                relativeLayout.setVisibility(View.GONE);
                tvEnd.setVisibility(View.GONE);
                break;
        }
        tvState.setText(temp);
        tvType.setText("类型：" + data.getType());
        tvContenTitle.setText(data.getTitle());
        tvContent.setText(data.getDescribe());
        seeAdapter.replaceData(data.getImg());

        // List<ReplyBean> da = new ArrayList<>();

        Collections.reverse(data.getReply());

        adapter.replaceData(data.getReply());
        //  adapter.replaceData(da);

        ns.post(() -> {
            ns.fullScroll(View.FOCUS_DOWN);
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 121 && resultCode == RESULT_OK) {
            getDetailsFormNet(service_id);
        }
    }
}
